import {getSessionItem, removeSessionItem, setSessionItem, ishasAgentId} from "@/basa/utils";

export default {
    data() {
        return {
            isActive: this.getIsActive(),
            listStatus: this.getListStatus(),
            formData: {},
            // 表格
            tableData: [],
            equipmentSettlementStatus: {
                "2": "待提交",
                "3": "审批中",
                "4": "已撤回",
                "6": "审批退回"
            },

            equipmentWriteOffStatus: {
                "2": "待提交",
                "3": "审批中",
                "4": "已撤回",
                "5": "审批通过",
                "6": "审批退回",
                "21": "冲销中",
                "22": "已冲销"
            },

            checkStateType: [
                { value: 2, label: "审批通过" },
                { value: 3, label: "审批退回" },
                { value: 5, label: "协办" },
                { value: 4, label: "审批不通过" },
                { value: 6, label: "转审" },
                { value: 5, label: "协办" }
            ],

            // settlementStatus 5 审批通过
            // writeoffStatus   1 冲销中   2 已冲销  0 审批通过
            // 分页
            page: {
                totalNum: 0,
                pageNoCode: 1,
                pageSizeCode: this.getLocalPageSize(),
                pageSizes: [10, 20, 50, 100, 200, 500]
            },
        }
    },
    methods: {
        // 分页方法
        handlePageChange(val, type) {
            if (type === "currentChange") {
                // 切换页码
                this.page.pageNoCode = val;
                this.getList();
            } else if (type === "sizeChange") {
                // 切换每页显示条数
                setSessionItem("equipment_size", 'pageSize',val);
                this.page.pageSizeCode = this.getLocalPageSize();
                this.getList();
            }
        },
        // 高级搜索
        search(searchData) {
            if (searchData.settlementStart) {
                searchData.settlementEnd = searchData.settlementStart[1];
                searchData.settlementStart = searchData.settlementStart[0];
            }
            this.page.pageNoCode = 1;
            searchData.pageNo = this.page.pageNoCode;
            searchData.pageSize = this.page.pageSizeCode;
            searchData.settlementStatus = this.listStatus;
            searchData.isShowPageLoading = true;
            this.getList(searchData);
        },
        // 高级搜索切换/更改触发
        change() {
            this.formData = {};
        },
        // 高级搜索重置
        resetForm() {
            this.page.pageNoCode = 1;
            this.formData = {};
            this.getList();
        },
        // 高级搜索去除空格
        handleTrim(key) {
            if (this.formData[key]) {
                this.$set(this.formData, key, this.formData[key].replace(/(^\s*)|(\s*)$/g, ""));
            }
        },
        // 页签切换
        toggleClick(v, i) {
            setSessionItem("equipment_isActive","isActive", i);
            setSessionItem("equipment_listStatus","listStatus", v.status);
            this.isActive = this.getIsActive();
            this.listStatus = this.getListStatus();
            removeSessionItem("equipment_size");
            // 切换时, 还原page, 删除session
            this.page.pageNoCode = 1;
            this.page.pageSizeCode = this.getLocalPageSize();
            this.getList();
        },
        // 默认显示多少条
        getLocalPageSize() {
            return getSessionItem("equipment_size", 'pageSize') || 10;
        },
        // 存储tabToggle状态
        getIsActive() {
            return getSessionItem("equipment_isActive","isActive") || 0
        },
        // 存储列表数据入参状态
        getListStatus() {
            return getSessionItem("equipment_listStatus","listStatus") || null
        },
        // 删除
        handleDelete(row, callBack) {
            this.$confirm("您确定要删除吗？", "删除确认", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                customClass: "ccui-message-box-custom"
            }).then(() => {
                callBack({id: row.id, isShowLoading: true}).then(res => {
                    this.$message({
                        type: "success",
                        message: res.data.message,
                        duration: 800,
                        onClose: () => {
                            this.getList();
                        }
                    });
                });
            });
        },
        // 撤回
        getRevoke(row, callBack) {
            this.$confirm("您确定要撤回吗？", "撤回确认", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                customClass: "ccui-message-box-custom"
            }).then(() => {
                callBack({id: row.id, isShowLoading: true}).then(res => {
                    this.$message({
                        type: "success",
                        message: res.data.message,
                        duration: 800,
                        onClose: () => {
                            this.getList();
                        }
                    });
                });
            });
        }
    },
    beforeRouteLeave(to, from, next) {
        removeSessionItem("equipment_isActive");
        removeSessionItem("equipment_listStatus");
        for (var key in this.$vnode.parent.parent.componentInstance.cache) {
            delete this.$vnode.parent.parent.componentInstance.cache[ishasAgentId(to.path, key)];
        }
        next();
    },
}